/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arthurh.android.BdeInfo.commande;
import com.arthurh.android.BdeInfo.connection.Connection;
import org.apache.http.message.BasicNameValuePair;
import java.util.*;
import android.widget.LinearLayout;
import org.apache.http.NameValuePair;
import android.widget.Button;
import android.content.Context;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;
/**
 *
 * @author arthurhalet
 */
public class Commande {
    Connection conn = new Connection();
    private final static int MIN_ETAPE_COMPLET = 2;
    private AlertDialog[] etapes;
    private int cursorEtapes = 0;
    private int currentEtape = 0;
    private Context c;
    private ArrayList<String> viewMenus;
    private HashMap<String, String> prepareCommande = new HashMap<String, String> ();
    
    public Commande(Context c){
        this.c = c;
    }
    public String getMenu(boolean seePriceSolo, boolean seePriceTotal){
        ArrayList<ArrayList<String>> menusList = this.getArray(1);
        String toReturn = "";
        for(ArrayList<String> menus : menusList){
                toReturn += menus.get(0);
                if(seePriceSolo){
                    toReturn += " ("+ menus.get(1) +"€)";
                }
                if(seePriceTotal){
                    toReturn += " ("+ menus.get(2) +"€)";
                }
                toReturn += "\n";
        }
        return toReturn;
    }
    public ArrayList<ArrayList<String>> getArray(int kind){
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("kind", ""+ kind));
        ArrayList<String> menusList = conn.connectionTo(nameValuePairs, "commande.php");
        String[] parse;
        
        
        ArrayList<ArrayList<String>> toReturn = new ArrayList<ArrayList<String>>();
        for(String menus : menusList){
            ArrayList<String> arrayMenus = new ArrayList<String>();
            parse = menus.split(":");
            if(parse.length >= 2){
                for(int i =0; i<parse.length; i++){
                    arrayMenus.add(parse[i]);
                }
            }
            toReturn.add(arrayMenus);
        }
        return toReturn;
    }
    private void nextDialog(){
        this.currentEtape = this.cursorEtapes;
        if(etapes[this.cursorEtapes] != null){
            this.cursorEtapes++;
        }
        if(currentEtape != this.cursorEtapes){
            etapes[this.currentEtape].show();
        }
        
        
    }
    private void precDialog(){
        this.cursorEtapes -= 2;
        if(this.cursorEtapes<0){
            this.cursorEtapes = 0;
        }
        
        Toast.makeText(c, ""+ this.cursorEtapes,Toast.LENGTH_SHORT).show();
        etapes[this.cursorEtapes].show();
        if(etapes[this.cursorEtapes] != null){
            this.cursorEtapes++;
        }
    }
    public void insertButton( LinearLayout layout, boolean seePriceSolo, boolean seePriceTotal){
        Button button;
        int nbEtapes = 0;
        ArrayList<ArrayList<String>> menusList = this.getArray(1);
        for(final ArrayList<String> menus : menusList){
            viewMenus = menus;
            button = new Button(c);
            button.setWidth(LayoutParams.FILL_PARENT);
            String text = menus.get(0);
            if(seePriceSolo){
                text += " ("+ menus.get(1) +"€)";
            }
            if(seePriceTotal){
                text += " ("+ menus.get(2) +"€)";
            }
            
            button.setText(text);
            button.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v) {
                    prepareCommande.put("idMenu", menus.get(3));
                    cursorEtapes = 0;
                    etapes = new AlertDialog[7];
                    etapes[0] = getDialogStyleMenu(menus.get(1), menus.get(2), Integer.parseInt(menus.get(4)), Integer.parseInt(menus.get(5)));
                    nextDialog();
                }
                
            });
            layout.addView(button);
        }
    }
    private AlertDialog getDialogStyleMenu( String priceSolo, String priceTotal, final int ingredient, final int sauce){
        prepareCommande.put("nameMenu", viewMenus.get(3));
        int nbEtapes = MIN_ETAPE_COMPLET;
        final CharSequence[] items = {"Menu simple ("+ priceSolo +"€)" , "Menu complet ("+ priceTotal +"€)"};

        final AlertDialog.Builder builder = new AlertDialog.Builder(c).setNegativeButton("Suivant", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                nextDialog();
           }
       }).setPositiveButton("Annuler", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });

        builder.setTitle("Choisissez une sorte de menu");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                prepareCommande.put("styleMenu", ""+item);
                int i = 1;
                if(ingredient>0){
                    etapes[i] = getDialogIngredient();
                    i++;
                }
                if(sauce>0){
                    etapes[i] = getDialogSauces();
                    i++;
                }
                
                if(item==1){
                    etapes[i] = getDialogBoissons();
                    i++;
                    etapes[i] = getDialogDesserts();
                }
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    private AlertDialog getDialogIngredient(){
        ArrayList<ArrayList<String>> ingredients = this.getArray(2);
        CharSequence[] items = new CharSequence[ingredients.size()];
        final int[] itemId;
        final ArrayList<Integer> chooseIngredient = new ArrayList<Integer>();
        itemId = new int[ingredients.size()];
        for(int i=0; i<items.length; i++){
            items[i] = ingredients.get(i).get(0);
            itemId[i] = Integer.parseInt(ingredients.get(i).get(1));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(c).setNegativeButton("Suivant", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               String listIngredient = "";
               for(int i =0; i<chooseIngredient.size();i++){
                   if(i>0){
                       listIngredient +=",";
                   }
                   listIngredient += chooseIngredient.get(i);
               }
               prepareCommande.put("ingredient", listIngredient);
               Toast.makeText(c, listIngredient,Toast.LENGTH_SHORT).show();
               dialog.cancel();
               nextDialog();
           }
       }).setPositiveButton("Précédent", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                precDialog();
           }
       });

        builder.setTitle("Choisissez les ingrédients (3):");
        builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    chooseIngredient.add(itemId[which]);
                    
                }else{
                    if(chooseIngredient.contains(itemId[which])){
                        chooseIngredient.remove(chooseIngredient.indexOf(itemId[which]));
                    }
                }
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    private AlertDialog getDialogSauces(){
        ArrayList<ArrayList<String>> sauces = this.getArray(3);
        CharSequence[] items = new CharSequence[sauces.size()];
        final int[] itemId;
        final ArrayList<Integer> chooseIngredient = new ArrayList<Integer>();
        itemId = new int[sauces.size()];
        for(int i=0; i<items.length; i++){
            items[i] = sauces.get(i).get(0);
            itemId[i] = Integer.parseInt(sauces.get(i).get(1));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(c).setNegativeButton("Suivant", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               String listSauce = "";
               for(int i =0; i<chooseIngredient.size();i++){
                   if(i>0){
                       listSauce +=",";
                   }
                   listSauce += chooseIngredient.get(i);
               }
               prepareCommande.put("sauce", listSauce);
               Toast.makeText(c, listSauce,Toast.LENGTH_SHORT).show();
               dialog.cancel();
               nextDialog();
           }
       }).setPositiveButton("Précédent", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                precDialog();
           }
       });

        builder.setTitle("Choisissez votre sauce (2):");
        builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    chooseIngredient.add(itemId[which]);
                    
                }else{
                    if(chooseIngredient.contains(itemId[which])){
                        chooseIngredient.remove(chooseIngredient.indexOf(itemId[which]));
                    }
                }
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    private AlertDialog getDialogBoissons(){
        ArrayList<ArrayList<String>> boissons = this.getArray(4);
        CharSequence[] items = new CharSequence[boissons.size()];
        final int[] itemId;
        itemId = new int[boissons.size()];
        for(int i=0; i<items.length; i++){
            items[i] = boissons.get(i).get(0);
            itemId[i] = Integer.parseInt(boissons.get(i).get(1));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(c).setNegativeButton("Suivant", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               
               dialog.cancel();
               nextDialog();
           }
       }).setPositiveButton("Précédent", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                precDialog();
           }
       });

        builder.setTitle("Choisissez votre boisson:");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                prepareCommande.put("boisson", ""+itemId[item]);
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    private AlertDialog getDialogDesserts(){
        ArrayList<ArrayList<String>> desserts = this.getArray(5);
        CharSequence[] items = new CharSequence[desserts.size()];
        final int[] itemId;
        itemId = new int[desserts.size()];
        for(int i=0; i<items.length; i++){
            items[i] = desserts.get(i).get(0);
            itemId[i] = Integer.parseInt(desserts.get(i).get(1));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(c).setNegativeButton("Suivant", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               
               dialog.cancel();
           }
       }).setPositiveButton("Précédent", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                precDialog();
           }
       });

        builder.setTitle("Choisissez votre dessert:");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                prepareCommande.put("dessert", ""+itemId[item]);
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
}
